<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="项目名称" prop="examName">
        <el-input
          v-model="queryParams.examName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="关键字" prop="examKey">
        <el-input
          v-model="queryParams.examKey"
          placeholder="请输入关键字"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="项目类型" prop="examType">
        <el-select
          v-model="queryParams.examType"
          placeholder="项目类型"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in examTypes"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="角色状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:exam_cost:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:exam_cost:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:exam_cost:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="Exam_costList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目费用ID" align="center" key="examId" prop="examId"  />
      <el-table-column label="项目名称" align="center" key="examName" prop="examName" :show-overflow-tooltip="true"  />
      <el-table-column label="关键字" align="center" key="examKey"  prop="examKey"  />
      <el-table-column label="项目单价" align="center" key="examOnePrice"  prop="examOnePrice"  />
      <el-table-column label="项目成本" align="center" key="examCost"  prop="examCost"  />
      <el-table-column label="单位" align="center" key="examUnit"  prop="examUnit"  />
      <el-table-column prop="examType" label="类别" align="center" key="examType"  :formatter="examTypeFormat" ></el-table-column>
      <el-table-column prop="status" label="状态" align="center" key="status"  :formatter="statusFormat" ></el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="depts" label-width="80px">

        <el-form-item label="项目类型" prop="status">
          <el-select
            v-model="form.examType"
            placeholder="项目类型"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in examTypes"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="examName">
          <el-input v-model="form.examName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="关键字" prop="examKey">
          <el-input v-model="form.examKey" placeholder="请输入关键字" />
        </el-form-item>
        <el-form-item label="项目价格" prop="examOnePrice">
          <el-input v-model="form.examOnePrice" placeholder="请输入项目价格" />
        </el-form-item>
        <el-form-item label="项目成本" prop="examCost">
          <el-input v-model="form.examCost" placeholder="请输入项目成本" />
        </el-form-item>
        <el-form-item label="项目单位" prop="examUnit">
          <el-input v-model="form.examUnit" placeholder="请输入项目单位" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
//import { listRole, getRole, delRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/system/role";
import { listExam_cost , addExam_cost , getExam_cost , updateExam_cost , delExam_cost} from "@/api/system/exam_cost"
import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept";

export default {
  name: "Exam_costs",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      examIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查项目表格数据
      Exam_costList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      examTypes:[],
      // 数据范围选项
      dataScopeOptions: [
        {
          value: "1",
          label: "全部数据权限"
        },
        {
          value: "2",
          label: "自定数据权限"
        },
        {
          value: "3",
          label: "本部门数据权限"
        },
        {
          value: "4",
          label: "本部门及以下数据权限"
        },
        {
          value: "5",
          label: "仅本人数据权限"
        }
      ],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examName: undefined,
        examKey: undefined,
        examType:undefined,
        status: undefined
      },
      // 表单参数
      form: {},

      // 表单校验
      depts: {
        DeptsName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "权限字符不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_exam_cost").then(response => {
      this.examTypes = response.data;
    });
  },
  methods: {
    /** 查询检查项目列表 */
    getList() {
      this.loading = true;
      listExam_cost(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.Exam_costList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false,
      this.menuNodeAll = false,
      this.deptExpand = true,
      this.deptNodeAll = false,
      this.form = {
        examId: undefined,
        examName: undefined,
        examKey: undefined,
        examOnePrice: undefined,
        examCost: undefined,
        examUnit: undefined,
        status: "0",
        deptIds: [],
        menuCheckStrictly: true,
        deptCheckStrictly: true,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.examIds = selection.map(item => item.examId)
      console.log(this.examIds)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加科室";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const examId = row.examId || this.examIds
      getExam_cost(examId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检查项目信息";
      });
    },


    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.examId != undefined) {
            updateExam_cost(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExam_cost(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const examIds = row.examId || this.examIds;
      this.$confirm('是否确认删除角色编号为"' + examIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delExam_cost(examIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },


    // 菜单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },

    // 菜单状态字典翻译
    examTypeFormat(row, column) {
      return this.selectDictLabel(this.examTypes, row.examType);
    }
  }
};
</script>
